﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Truextend.Training.Model.Entities;
using Truextend.Training.Presenter;
using Truextend.Training.Presenter.ViewInterfaces;
using System.Collections.Generic;
using Microsoft.SharePoint;
using System.Web;

namespace Truextend.Training.SubjectList
{
    public partial class SubjectListUserControl : UserControl, IListCourseView
    {
        ListCoursePresenter presenter;
        List<Course> courseList;
        SPUser user;

        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;
            user = web.CurrentUser;

            presenter = new ListCoursePresenter(this);
            presenter.InitView();

            DisableButtons();

            if (courseList != null)
            {
                HLnkCalendar.NavigateUrl = SPContext.Current.Web.Url + "/_layouts/Truextend.Training/Pages/CoursesCalendarPage.aspx";
                HLnkCalendar.Visible = true;

                HLnkInstances.NavigateUrl = SPContext.Current.Web.Url + "/_layouts/Truextend.Training/Pages/CourseManageListPage.aspx";
                HLnkInstances.Visible = true;
            }
        }

        public string Message
        {
            set { lblMessage.Text = value; }
        }

        public IList<Course> Courses
        {
            set
            {
                this.courseList = new List<Course>(value);
                gvCourses.DataSource = this.courseList;
                gvCourses.DataBind();
            }
        }

        protected void LnkDelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                long courseId = Convert.ToInt64(e.CommandArgument);
                presenter.DeleteCourse(courseId);

                string url = HttpContext.Current.Request.Url.AbsoluteUri;
                Response.Redirect(url);
            }
        }

        protected void LnkNewVersion_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "NewVersion")
            {
                long courseId = Convert.ToInt64(e.CommandArgument);

                string url = SPContext.Current.Web.Url + "/_layouts/Truextend.Training/Pages/CourseFormPage.aspx?courseid=" + courseId;
                Response.Redirect(url);
            }
        }

        private void DisableButtons()
        {
            SPGroupCollection userGroups = user.Groups;
            List<string> groups = new List<string>();
            foreach (SPGroup group in userGroups)
            {
                 groups.Add(group.Name);
            }
            if (!groups.Contains("Education Managers"))
            {
                foreach (GridViewRow row in gvCourses.Rows)
                {
                    row.FindControl("tblOptions").Visible = false;
                }
                HLnkNewCourse.Visible = false;
            }
        }
    }
}
